package com.fclub.push.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javapns.devices.Device;

import javax.annotation.Resource;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import com.fclub.push.dao.PushDao;
import com.fclub.push.vo.PushDeviceVo;
import com.fclub.push.vo.PushInfoVo;

/**
 * 获取推送数据 dao 实现类
 * 
 * @author penghui.li
 * 
 */
public class PushDaoImpl extends SqlSessionDaoSupport implements PushDao {

	
	@Resource
	private JdbcTemplate push_db;
	/**
	 * 获取推送数据
	 * 
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<PushInfoVo> queryPushInfoList(Map map) {
		List<PushInfoVo> list = this.getSqlSession().selectList(
				"com.fclub.push.vo.PushMessVo.queryPushInfo", map);
		return list;
	}

	/**
	 * 获取推送udid
	 * 
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<PushDeviceVo> queryPushUdidList(Map map) {
		List<PushDeviceVo> list = null;
		if (map.get("type").equals("0")) {
			list = this.getSqlSession().selectList(
					"com.fclub.push.vo.PushMessVo.queryPushDevice", map);
		} else {
			list = this.getSqlSession().selectList(
					"com.fclub.push.vo.PushMessVo.queryPushDevice1", map);
		}
		return list;
	}
	
	
	public void updatePushInfoStatus(final List<PushInfoVo> list){
		String sql = "update fc_flc_mobile_push set status=1 where id =?";
		//批处理
		BatchPreparedStatementSetter set = new BatchPreparedStatementSetter(){
			public int getBatchSize() {
				return list.size();
			}	
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				PushInfoVo bean = list.get(i);
				ps.setLong(1, bean.getId());
		}};
		this.push_db.batchUpdate(sql, set);
	}
	
	
//	/**
//	 * 更新推送数据的推送状态
//	 * 
//	 * @param map
//	 */
//	@SuppressWarnings("rawtypes")
//	public void updatePushInfoStatus(Map map) {
//		this.getSqlSession().update(
//				"com.fclub.push.vo.PushMessVo.updatePushInfoStatus", map);
//	}
	
	/**
	 * 更新推送设备的状态 
	 * @param map
	 */
	public void updateDeviceStatus(final List<Device> feedBacklist){
		String sql = "	update fc_flc_ios_devicetoken set status=0 where devicetoken =?";
		//批处理
		BatchPreparedStatementSetter set = new BatchPreparedStatementSetter(){
			public int getBatchSize() {
				return feedBacklist.size();
			}	
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Device bean = feedBacklist.get(i);
				ps.setString(1, bean.getToken());
		}};
		this.push_db.batchUpdate(sql, set);
	}
}
